/**
*	oa审批事项列表页面
*
*/

define(['jquery','init','dataTable','dataTables-jqueryui','jqueryValidate','bsUtil',
			'bsButton','bsButtonEvent','bsMsgServ','formServ','bsUtil','codeDef','datagridServ'
			,'moment','bootstrap-datetimepicker','bootstrap-datetimepicker.zh-CN','dateUtil','oaMainList'],
    	function($,init,dataTable,ui,validate,bsUtil,
    				bsButton,bsButtonEvent,bsMsgServ,formServ,bsUtil,codeDef,datagridServ
    				,moment,datetimepicker,datetimepickerLang,dateUtil,oaMainList) {
		
    	//页面根节点
    	var rootid = 'issue-main',
    	
    	//查看页面根节点
    	viewRootid = 'issue-list',
    	
    	//detail 按钮容器
    	detail_btn_container = rootid + ' .btn-container',
    	
    	//验证公用属性
    	ruleBase = {
			errorElement : 'span',  
            errorClass : 'js-required-field',  
            focusInvalid : true
    	}
    	,
    	//当前业务字段验证规则
    	ruleConfig
    		,
    	//业务列表-表单容器对象
    	list_c = ' .list-detail',
    	
    	//当前表单对象
    	current_form,
    	
    	//脚本传参模式
    	isScriptOpen = false,
    	
    	//查看模式显示标志
    	isView = false,
    		
    	//当前业务模块代码
    	current_bizcode, 

    	//当前模块配置
    	current_config,
    	
    	//当前选中行
    	curent_selectRow ={},
    	
    	//额外数据
    	external_data = {},
    	
    	//脚本传参临时变量
    	scriptParam ,    	
    	
    	//字段配置url
    	field_url = '/flowTaskConfig/query/getTaskConfig.action'
    	
		var private_method = {
    		
			/**
			 * 附加子表数据回调函数
			 * @param _options.field	属性名称
			 * @param _options.value	属性值
			 */
			setExternalData:function(_options){
				if(_options && _options.field){
					external_data[_options.field]=_options.value
				}
			}
			,
    	/**
    	 * 初始化侧边栏导航
    	 */
    	initNav:function(){
    		var _param = {
    			url:'/'+init.getSite()+'/permission/query/getUserDutiesMenu.action',
    			success:function(data){
    				if(window.console){
    					console.log('menu data: ' + data.length);
    				}
    				var _el = $('#' + rootid + ' #left-nav')
    				if(_el.length==1){
    					
    					//设定导航栏高度
    					var _navCss = {
    						"overflow"  : "auto",
    						"overflow-x": "hidden", 
    						"height"    : init.getGridHeight()+'px'} 
    					$(_el).css(_navCss);
    					
    					var _html = '';
    					$.each(data,function(i,o){
    						
    						_html+='<li>'+
    							   
    							   '<a class="left-nav-btn" href="javascript:void(0);" id="'+o.bizcode+'">'+ 
    							   '<span class="'+o.iconcls+'">' + o.pre_name +'</span>'+
    							   ' ' + o.short_name +
    							   '<span class="badge badge-'+ o.bizcode+'-notify"></span>'+
    							   '</a>'+
    							   '</li>'
    						
    					})
    					
    					if(_html){
    						_el.empty();
    						_el.append(_html);
    						
    						private_method.btn_listenner();
    						
    						bsUtil.updateIssueBadge();
    					}
    				}
    			}
    		}
    		$.ajax(_param)
    	},
    	/**
    	 * 初始化列表
    	 * @param _options.module	模块代码
    	 * @param _options.ajax		server端数据访问方法
    	 * @param _options.paging	分页标志
    	 * @param _options.columns	缺省列
    	 * @param _options.queryParam	缺省查询条件
    	 * @param _options.rootid	dom跟节点id
    	 */
    	initList:function(_options){
    		
    		 var _param ={
    		 	height       : init.getGridHeight(),
    		 	rownumbers   : true,
				animate      : true,									
				striped      : true,
				border       : true,
				//singleSelect:false,
				idField      : 'id',								
				fitColumns   : true,
				checkOnSelect: true,					
				selectOnCheck: true,
				pagination   : true,
				url          : '/'+init.getSite()+'/'+_options.module+'/query/getOperateTaskListDomain.action',
				columns      : [[
					{field:'ck',checkbox:true},
					{field:'bizno',title:'业务编码',width:50,halign:'center'},        
					{field:'name',title:'事项名称',width:100,halign:'center'},        
					{field:'apply_deptname',title:'申请科室',width:50,halign:'center'} ,
					{field:'apply_name',title:'申请人员',width:50,halign:'center'} ,
					{field:'apply_date',title:'申请时间',width:50,halign:'center'},
					{field:'flowState',title:'事项状态',width:50,halign:'center'} 
//					{field:'apply_name',title:'操作',width:50,align:'center',
//						formatter:function(value,row,index){
//							return  '<span><font color=blue>下载</font></span>';
//						}
//					}
					
				]],
				onDblClickRow: private_method.dblClick
    		 }
    		 
    		 if(_options.queryParam){
    		 	
    		 	_param.queryParams = {condition:_options.queryParam};
    		 }
    		 
    		 var dg = $('#' + rootid + ' .dg-list')
    		 if(_options.rootid){
    		 	dg = $('#' + _options.rootid + ' .dg-list')	
    		 }
    		 if(dg.length>0){
    		 	dg.datagrid(_param)
    		 }
    		
    	}
    	,
    	/**
    	 * 双击事件
    	 */
    	dblClick:function(index,data){
    		var dg = $('#' + rootid + ' .dg-list')
    		dg.datagrid('clearSelections');
    		dg.datagrid('selectRow',index);
    		var _btn = $('#' + rootid + ' [btn-type="listEditReplace"]');
    		_btn.trigger('click');
    		
    	}
    	,
    	/**
    	 * 初始化
    	 */
    	initEdit:function(_options){
    		
    		//清理遗留数据
    		
    		//绑定按钮事件，redraw函数完成  		
    		
    		
    		if(_options.beforeInit){
    			var _data = $.extend({},curent_selectRow,external_data)
    			_options.beforeInit({
    				row:_data,
    				callbackFunc:private_method.setExternalData
    			});		
    		}
    		
    		//脚本显示页面，按钮事件单独绑定
    		if(isScriptOpen){
    			
    			
    			var _param = {
        			bizcode: current_bizcode,
        			btnType: 'edit',
        			container: rootid + ' .btn-container'
        		}
    			if(isView){
    				_param.btnType = 'view';
    			}
    			private_method.redrawBtn(_param);
    		}
    		
    		//初始化表单元素
    		private_method.initForm();
    		
    		//初始化部门、人员及时间信息
    		if(!isScriptOpen){
    			private_method.initDeptUser();
    		}
    		
    		//初始化签名组件
    		private_method.initSign();
    		
    		//初始化金额组件
    		private_method.initMoney();
    		
    		//获取字段验证规则,显示必填项
    		if(!isView){
    			private_method.getFieldConfig();
    		}else{
    			//初始化附件预览
				var _param = {
	    			bizcode 	: current_bizcode,
	    			rootid  	: rootid,
	    			containerid : 'archive-container',
	    			bizno		: curent_selectRow.id
	    		}		    		
				bsUtil.initArchive(_param)	
    		}
    		
    		
    		//判断是否存在自定义初始化函数
    		if(_options.initElement){
    			var _data = $.extend({},curent_selectRow,external_data)
    			_options.initElement({
    				row:_data,
    				callbackFunc:private_method.setExternalData
    			});	
    		}
    	}
    	,
    	/**
    	 * 按钮事件定义
    	 */
    	btn_listenner:function(){
    		var _btn = $('#'+rootid +' a.left-nav-btn')
    		if(_btn.length>0){
    			_btn.unbind('click');
    			_btn.bind('click',private_method.navClick)
    		}
    	}
    	,
    	
    	initDeptUser:function(){
    		var usr = init.getCurrentUser();
					
			if(usr){
				//初始化部门信息
				var _elid = $('#'+rootid + ' .main-form #apply_deptid')
				var _elname = $('#'+rootid + ' .main-form #apply_deptname')
				var _val
				if(_elname.length==1){
					_val = _elname.val();
					if(!_val){
						_elname.val(usr.deptName);
						_elid.val(usr.deptid);
					}
				}
				
				//初始化人员信息
				_elid = $('#'+rootid + ' .main-form #apply_id')
				_elname = $('#'+rootid + ' .main-form #apply_name')
				var _val
				if(_elname.length==1){
					_val = _elname.val();
					if(!_val){
						_elname.val(usr.name);
						_elid.val(usr.id);
					}
				}
				
				//初始化申请时间
				var _elDate = $('#'+rootid + ' .main-form #apply_date')
				
				var _param ={
						language:  'zh-CN',
						format: "yyyy-mm-dd hh:ii:ss",
						autoclose: true,
						minView: "month",
						maxView: "decade",
						todayBtn: true,
						pickerPosition: "bottom-left"
					}
				_elDate.datetimepicker(_param);
				var _elVal
				if(_elDate.length==1){
					_elVal = _elDate.val();
					if(!_elVal){
						_elDate.val(dateUtil.getDateValue({format:'yyyy-MM-dd hh:mi:ss'}))	
					}
				}
				
				//初始化日期字段
				var _elDate = $('#'+rootid + ' .main-form .single-date')
				
				if(_elDate.length==1){
					var _param ={
							language:  'zh-CN',
							format: "yyyy-mm-dd",
							autoclose: true,
							minView: "month",
							maxView: "decade",
							todayBtn: true,
							pickerPosition: "bottom-left"
						}
					_elDate.datetimepicker(_param);
//					var _elVal = _elDate.val();
//					if(!_elVal){
//						_elDate.val(dateUtil.getDateValue({format:'yyyy-MM-dd'}))	
//					}
				}
				
				//初始化时间字段
				var _elDate = $('#'+rootid + ' .main-form .single-time')
				if(_elDate.length>=1){
					var _param ={
							language:  'zh-CN',
							format: "yyyy-mm-dd hh:ii:ss",
							autoclose: true,
							minView: "month",
							maxView: "decade",
							todayBtn: true,
							pickerPosition: "bottom-left"
						}
					_elDate.datetimepicker(_param);
//					var _elVal = _elDate.val();
//					if(!_elVal){
//						_elDate.val(dateUtil.getDateValue({format:'yyyy-MM-dd hh:mi:ss'}))	
//					}
				}
				
			}
    	}
    	,
    	/**
    	 * 初始化签名组件
    	 */
    	initSign:function(){
    		var _el = $('#'+rootid + ' .main-form .sign-control')
    			
    		if(_el.length>0){
    			_el.unbind('click');
    			_el.bind('click',private_method.signClick)
    		}
    	}
    	,
    	
    	initMoney	:function(){
    		var _el = $('#'+rootid + ' .main-form .money-control')
    			
    		if(_el.length>0){
    			_el.unbind('keyup');
    			_el.bind('keyup',bsUtil.moneyKeyup)
    		}
    	}
    	,
    	signClick:function(e){
    		var _el = $(e.currentTarget);
    		var _timeEl
    		
    		//判断当前字段
    		if(_el.length==1){
    			
    			var _ro = _el.attr('readonly');
    			if(!(typeof(_ro)=="undefined")){
    				return ;	
    			}
    			
    			//获取字段前缀
    			var _id_prefix='';
    			var  fid_prefix= _el.attr('id');
    			if(fid_prefix){
    				_id_prefix = fid_prefix.substr(0,fid_prefix.length-8);
    			}
    			
    			//查找当前字段父节点
    			var _parent = _el.closest('.row-border');
    			
    			if(_parent.length==1){
	    			//赋值用户名及id
	    			var usr = init.getCurrentUser();
	    			
	    			//查找用户名字段
	    			var _uel = $(_parent).find('.sign-user');
	    			if(_uel.length==1){
	    				_uel.val(usr.name);
	    				
	    				
	    				//查找用户id字段
	    				var _idEl = $('#'+rootid + ' .main-form #'+_id_prefix + '_id');		    				
	    				if(_idEl.length==1){
	    					_idEl.val(usr.id);	
	    				}
	    				
	    				//查找用户部门id字段
	    				var _idEl = $('#'+rootid + ' .main-form #'+_id_prefix + '_deptid');		    				
	    				if(_idEl.length==1){
	    					_idEl.val(usr.deptid);	
	    				}
		    			
	    			}  			
    			
    				//查找签名时间字段
    				var _tel =$(_parent).find('.sign-time');
    				
    				//判断签名时间字段是否存在
    				if(_tel.length==1){    					    						
						$(_tel).val(dateUtil.getDateValue({format:'yyyy-MM-dd hh:mi:ss'}))    					
    				}
    			}
    		}
    	}
    	,
    	/**
    	 * 业务类型点击事件
    	 */
    	navClick:function(e){
    		
    		//选中状态 class 调整
    		$('#' + rootid + ' #left-nav li').removeClass('active');
    		$(e.currentTarget).closest('li').addClass('active');
    		
    		//清除缓存子表数据
    		external_data = {};
    		
    		//获取选中对象类型
    		current_bizcode = init.getNavType(e);
    		if(window.console){
				console.log('module: ' + current_bizcode);
			}
    		
    		//获取容器对象
			var _c =$('#' + rootid +list_c)
    		
			//刷新容器对象，变更数据
			if(_c){
				//清除原有内容
				_c.empty();
				
				var _html = '<table class="dg-list" cellspacing="0" width="100%"></table>';
				_c.append(_html);
			}			
			
			private_method.redrawBtn({
				bizcode:current_bizcode,
				btnType:'list',
				bizType:'flow',
				container: detail_btn_container
			})
    		private_method.initList({module:current_bizcode});
    	}
    	,
    	
    	//外部调用初始化流程列表
    	/**
    	 * @param _options.bizcode		流程业务代码
    	 * @param _options.rootid		dom跟节点id
    	 * @param _options.queryParam	初始查询参数
    	 * @param _options.btnContainer	按钮容器对象
    	 * @param _options.listContainer 列表页面容器对象
    	 */
    	initOutList:function(_options){
    		//获取容器对象
			var _c =$('#' + _options.rootid + _options.listContainer)
    		
			//刷新容器对象，变更数据
			if(_c){
				//清除原有内容
				_c.empty();
				
				var _html = '<table class="dg-list" cellspacing="0" width="100%"></table>';
				_c.append(_html);
			}	
			
			private_method.redrawBtn({
				bizcode:current_bizcode,
				btnType:'list',
				bizType:'flow',
				container: _options.rootid + ' .btn-container'
			})
			
			private_method.initList({
				module		: current_bizcode,
				queryParam	: _options.queryParam,
				rootid		: _options.rootid
			});
    	},
    	
    	clearDetail:function(){
    		var _c =$('#' + rootid +list_c)
    			
    			if(_c){
    				//清除原有内容
    				_c.empty();
    			}
    	}
    	
    	,
    	
    	/**
    	 * 更新按钮
    	 * @param _options.bizcode	业务编码
    	 * @param _options.btnType		按钮类型(list,edit,view)
    	 * @param _options.bizType	业务类型(0:normal,1:flow,2:audit)
    	 * @param _options.container 按钮容器对象
    	 */
    	redrawBtn:function(_options){
    		
    		
    		var _btn ;
    		if(_options && _options.btnType){    			
	    		_btn = bsButton.getBtn(_options);	    				
    		}    		
    		
    		$('#'+_options.container).empty();
    		
    		if(_btn){    			
    			$('#'+_options.container + ' button').unbind('click');    			
    			$('#'+_options.container).append(_btn);    			
    			//绑定按钮事件
    			$('#'+_options.container + ' button').bind('click',private_method.btnClick);
    		}
    		
    	}
    	,
    	
    	reDrawBizBadge:function(){
    		var _el = $('#'+rootid + ' .active span')
    		
    		var grid = $('#'+rootid + ' .dg-list')
    		
    		var _o = grid.datagrid('options')
    		var _cnt = _o.total
    		_el.html(_cnt);
    		
    	}
    	,
    	btnClick:function(e){
    		
    		init.applyBtnEvt({btnEvt:e,btnConfig:btnEvent})
//    		bsMsgServ.msgSlide();
    	}
    
    	
    	

    	,
    	/**
    	 * 初始化表单页面元素
    	 * @param _options.code		
    	 */
    	initForm:function(_options){
    		
    		
    		var _f = $('#'+rootid + list_c + ' .main-form')
    		
    		//判断是否传入目标form对象
    		if(isScriptOpen){
    			_f = $('#'+rootid + ' .main-form')
    		}
    		
    		//业务id处理			
			if(curent_selectRow ){				
				if(!curent_selectRow.id || curent_selectRow.id==''){
					//id为空,赋值临时id
					var _date = new Date();
					var _id = 'TMP' +current_bizcode+ _date.getFullYear()+''+(_date.getMonth()+1)+''+_date.getDate()+'' +_date.getMilliseconds();
					curent_selectRow.tmpid = _id;
					curent_selectRow.id = _id;
				}
			}else{
				var _date = new Date();
				var _id = 'TMP' +current_bizcode+ _date.getFullYear()+''+(_date.getMonth()+1)+''+_date.getDate()+'' +_date.getMilliseconds();
					
				curent_selectRow = {
					tmpid	: _id,
					id		: _id
				}	
			}
			
    		if(window.console){
    			console.log(' issue form length:  '+ _f.length)	
    		}
    		if(_f.length==1 ){    			
    			_f.form('load',curent_selectRow);    			 			
    		}else{
    			oaMainList.initForm();     			
    		}
    		
    	}
    	
    	,
		/**
		 * 获取字段必填、可填配置
		 * 
		 */
		getFieldConfig:function(){
			//查询获取模块节点配置			
			var task_config_url = '/'+init.getSite() + field_url;
			var task_param = {}
			
			current_config = init.getBizConfig({bizcode:current_bizcode})
			if(current_config && current_config.fullBizcode){
				task_param.fullBizcode = current_config.fullBizcode;
			}
			
			//当前是否选中行
			if(curent_selectRow && curent_selectRow.task_def_key){
				task_param.activityId=curent_selectRow.task_def_key
			}
			
			$.ajax({
				url:task_config_url,
				data:task_param,
				type : 'post',
				success:private_method.fieldSetting
			})
		}
		
		,
		/**
		 * 设置form字段
		 */
		fieldSetting:function(data){
			
			var archiveEanbled = false;
			
			
			if(data){						

				var _f = data.editFilter

				var fields

				if(_f && _f.enabled){
					//旧模块流程定义配置模式
					fields = _f.enabled.split(',')
				}else{
					//新模块流程定义配置模式
					if(data.requiredFilter){
						fields = data.requiredFilter.split(',')
					}
				}

				if(fields && (fields.length>0)){
					var rules={},messages={}
					for(var i =0;i<fields.length;i++){
						var _el =$('#' + rootid + ' .main-form #' + fields[i])
						if(_el.length>0){
							 $(_el).addClass('js-required-field')
							 $(_el).removeAttr('readonly')
							 rules[fields[i]]='required';
							 messages[fields[i]]='字段不允许为空';
						}
					}
					ruleConfig = {rules:rules,messages:messages}
//					$($form).form('enableValidation').form('validate')
				}
				
				//可编辑字段
				if(data.editableFilter){
					fields = data.editableFilter.split(',')
					if(fields && (fields.length>0)){
						for(var i=0;i<fields.length;i++){
							var _el =$('#' + rootid + ' .main-form #' + fields[i])
							if(_el.length>0){							 
								$(_el).removeAttr('readonly')							 
							}
						}
					}
				}				
				
				//附件启用标志
				archiveEanbled = data.archiveEnabled
				
			}
			
			
			//附件初始化功能
			var _idEl = $('#'+rootid + ' #id')
			var _id
			if(_idEl.length==1){				
				_id = _idEl.val();
			}
			
			
			
			if(_id){
				//编辑页面
				if(archiveEanbled){
					//初始化附件上传
		    		bsUtil.initUpload({
		    			bizcode 	: current_bizcode,
		    			rootid  	: rootid,
		    			containerid : 'archive-container',
		    			bizno   	: _id,
		    			del			: true
		    		})
				}else{
					//初始化附件预览
					var _idEl = $('#'+rootid + ' #id')
					var _id
					if(_idEl.length==1){				
						_id = _idEl.val();
					}
	
					if(_id){
						var _param = {
							bizcode 	: current_bizcode,
							rootid  	: rootid,
							containerid : 'archive-container',
							bizno		: _id
						}		    		
						bsUtil.initArchive(_param)
					}
				}
			}else{
				//查询统计页面
				_idEl = $('#'+viewRootid + ' #id')
				if(_idEl.length==1){
					_id = _idEl.val();	
					bsUtil.initArchive({
		    			bizcode 	: oaMainList.getCurrentBizcode(),
		    			rootid  	: viewRootid,
		    			containerid : 'archive-container',
		    			bizno		: _id
		    		})
				}	
			}
		}
		,
		validateForm:function(e){
			
			var _f = $('#'+rootid + ' .main-form')
			if(
				!formServ.validateForm({
					form:_f,
					rules:ruleConfig
				})
			){
				bsMsgServ.errorSlide('请填写表单内容');
				//释放按钮禁用状态
				$(e.currentTarget).removeClass('disabled');
				return 0
			}			
			
			return 1
		}
		,
		//获取form数据
		getFormData:function(){
			var _f = $('#'+rootid + ' .main-form')
			var formData = formServ.getDataObj({
						form : _f
					});
					
			//获取子表信息
			if(external_data){
				formData = $.extend({},formData,external_data)
			}
			//流程信息
			if(current_config.flowid){
				formData.flowid = current_config.flowid
				formData.fullBizcode = current_config.fullBizcode
			}
			
			//业务编码
			if(!formData.bizcode){
				var _bizcode = current_config.bizcode
				formData.bizcode = _bizcode
			}
			
			if(formData.id && (formData.id).substring(0,3)=='TMP'){				
				formData.id = null;	
			}
			return formData;
		}
		,
		triggerNav:function(){
			var _btn = $('#' + rootid + ' .active a')
    		_btn.trigger('click');	
		}
		
	};
	
	var btnEvent = {
		
		//当前页新增
		listAddReplace:function(e){
    		
			//清除缓存数据
			curent_selectRow = null;
    		var _url = '/common/query/getBizPage.action?subSystem=oa&code=bsEditDlg&module='+current_bizcode
    		
    		
    		//新增界面参数
    		var _lParam = {
    			url:_url,
    			container:'issue-main .list-detail',
    			formInit:null,
    			evt:e
    		}
    		if(scriptParam){
    			//form容器对象重定义
    			if(scriptParam.listContainer){    				
    				_lParam.container =  scriptParam.rootid + ' ' + scriptParam.listContainer;
    			}   			
    		}
    		bsButtonEvent.add(_lParam)
    		
    		//按钮刷新参数
    		var _bParam ={
    			bizcode: current_bizcode,
    			btnType: 'edit',
    			container: detail_btn_container
    		}
    		
    		if(scriptParam && scriptParam.btnContainer){
    			_bParam.container = scriptParam.rootid + ' ' + scriptParam.btnContainer	
    		}
    		
    		private_method.redrawBtn(_bParam)
    		
    		
			if(scriptParam && scriptParam.formData){
				curent_selectRow = scriptParam.formData;
			}   
    	}
    	,
    	
    	//弹窗新增
    	listAddPop:function(e){
    		var _url = '/common/query/getBizPage.action?subSystem=oa&code=bsEditDlg&module='+current_bizcode
    		bsButtonEvent.add({
    			url:_url,
    			container:'issue-main .list-detail',
    			formInit:null,
    			evt:e
    		})
    	}
    	,
    	//当前页编辑
    	listEditReplace:function(e){
    		
    		var _record = $('#' + rootid + ' .dg-list').datagrid('getSelections');
    		
    		if(scriptParam && scriptParam.rootid){
    			_record = $('#' + scriptParam.rootid + ' .dg-list').datagrid('getSelections');
    		}
    		
    		if(_record.length==1){
	    		var _url = '/common/query/getBizPage.action?subSystem=oa&code=bsEditDlg&module='+current_bizcode
	    		
	    		//新增界面参数
	    		var _lParam = {
	    			url:_url,
	    			container:'issue-main .list-detail',
	    			formInit:null,
	    			data:_record,
	    			evt:e
	    		}
	    		if(scriptParam && scriptParam.listContainer){
	    			_lParam.container =  scriptParam.rootid + ' ' + scriptParam.listContainer	
	    		}
    			bsButtonEvent.add(_lParam)
	    		 
	    		//按钮刷新参数
	    		var _bParam ={
	    			bizcode: current_bizcode,
	    			btnType: 'edit',
	    			container: detail_btn_container
	    		}
	    		
	    		if(scriptParam && scriptParam.btnContainer){
	    			_bParam.container = scriptParam.rootid + ' ' + scriptParam.btnContainer	
	    		}
	    		
	    		private_method.redrawBtn(_bParam)
	    		
	    		//暂存当前选中行
	    		curent_selectRow = _record[0];
    		}else{
    			if(_record.length>1){
    				bsMsgServ.warnSlide('您选择了多条记录，请鼠标双击想编辑的行.')	
    			}else{
    				bsMsgServ.warnSlide('请选择想操作的行')	
    			}
    			$(e.currentTarget).removeClass('disabled');
    		}
    		
    		
    	}
    	
    	,
    	listDel:function(e){
    		var _grid = $('#' + rootid + ' .dg-list')
    		
    		if(scriptParam && scriptParam.rootid){
    			_grid = $('#' + scriptParam.rootid + ' .dg-list');
    		}
    		var _record = _grid.datagrid('getSelections');
    		if(_record.length>0){
    			datagridServ.delRow({
    				url:'/'+init.getSite()+'/'+ current_bizcode +'/flowDel.action',
    				target:_grid,
    				callback:function(data){
    					if(data){
    						if(data.error){
    							var _param = {type:'list'}
    							switch(data.error){
    								case 'data':
    									_param.code='data'
		    							
	    								break;
	    							case 'audit':
	    								_param.code='audit'
	    								break;
    							}
    							bsMsgServ.warnSlide(_param);
    							
    						}else{
    							bsMsgServ.msgSlide(
	    							codeDef.getConfig({
	    								type:'operate',
	    								code:'success'
	    							})	
    							)
    							
    							//判断是否外部调用列表页面，非外部调用页面刷新提示数字
    							if(!scriptParam){
    								bsUtil.updateIssueBadge();
    							}
    						}
    					}
    				}
    			})
    		}else{
    			bsMsgServ.warnSlide('请选择想操作的行');
    		}
    		$(e.currentTarget).removeClass('disabled');
    	}
    	,
    	//弹窗编辑
    	listEditPop:function(e){
    		
    		
    	}
    	,
    	//取消编辑
    	editCancelReplace:function(){
    		private_method.triggerNav();
    	}
    	,
    	// 提交form
		editSaveReplace : function(e) {
			
			if(window.console){
				console.log(' save fired ...')	
			}
			var _f = $('#'+rootid + ' .main-form')
			
//			if(!private_method.validateForm(e)){
//				//提示和释放按钮在函数中处理
//				
//				//form验证未通过，中断函数执行并返回
//				return
//			}			
			
			var formData = private_method.getFormData();	
			
			//脚本传入的表单扩展参数
			if(scriptParam && scriptParam.formData){
				$.extend(formData,scriptParam.formData);	
			}
			
			$.ajax({
				url:'/'+init.getSite()+'/'+ current_bizcode +'/flowSave.action',
				data:$.toJSON(formData),
				contentType:'appliction/json;charset=utf-8',
				type:'post',
				dataType:'json',
				success:function(data){
					if(data){						
						//刷新form
						
						var _form = $('#'+rootid + ' .main-form')
						if(scriptParam && scriptParam.rootid){
							_form = $('#'+scriptParam.rootid + ' .main-form')
						}
						_form.form('load',data);				
						
					}
					
					if(scriptParam && scriptParam.saveCallback){
						scriptParam.saveCallback(data);
					}else{
						bsMsgServ.msgSlide(codeDef.getConfig({
							type:'operate',
							code:'success'
						}))
						bsUtil.updateIssueBadge();
					}
					
					$(e.currentTarget).removeClass('disabled');
				}
				,
				error:function(){
					bsMsgServ.errorSlide(codeDef.getConfig({
						type:'operate',
						code:'error'
					}))
					$(e.currentTarget).removeClass('disabled');
				}
			})
							
		}
		,
		/**
		 * 流程发送
		 */
		editRedoReplace:function(e){
			if(window.console){
				console.log(' redo fired ...')	
			}
			var _f = $('#'+rootid + ' .main-form')
			if(!private_method.validateForm(e)){
				//提示和释放按钮在函数中处理
				
				//form验证未通过，中断函数执行并返回
				return
			}			
			
			var formData = private_method.getFormData();		
			
			$.ajax({
				url:'/'+init.getSite()+'/'+ current_bizcode +'/flowTaskComplete.action',
				data:$.toJSON(formData),
				contentType:'appliction/json;charset=utf-8',
				type:'post',
				dataType:'json',
				success:function(data){
					if(data){						
						
						
						if(scriptParam && scriptParam.sendCallback){
							scriptParam.sendCallback(data);
						}else{
							//刷新列表
							private_method.triggerNav();
							
							//刷新提示数字
							bsUtil.updateIssueBadge();
							
							private_method.reDrawBizBadge();
							
							bsMsgServ.msgSlide(codeDef.getConfig({
								type:'operate',
								code:'success'
							}))
						}
					}
					
					
					
				}
				,
				error:function(){
					bsMsgServ.errorSlide(codeDef.getConfig({
						type:'operate',
						code:'error'
					}))
					$(e.currentTarget).removeClass('disabled');
				}
			})
		}
		,
		//流程回退
		editUndoReplace:function(e){
			if(window.console){
				console.log(' undo fired ...')	
			}
			var _f = $('#'+rootid + ' .main-form')
						
			var formData = private_method.getFormData();		
			
			$.ajax({
				url:'/'+init.getSite()+'/'+ current_bizcode +'/flowSendback.action',
				data:$.toJSON(formData),
				contentType:'appliction/json;charset=utf-8',
				type:'post',
				dataType:'json',
				success:function(data){
					if(data){						
						if(scriptParam && scriptParam.backCallback){
							scriptParam.backCallback(data);
						}else{
							//刷新list
							private_method.triggerNav();
							
							//刷新提示数字
							bsUtil.updateIssueBadge();
							
							private_method.reDrawBizBadge();
							
							bsMsgServ.msgSlide(codeDef.getConfig({
								type:'operate',
								code:'success'
							}))
						}
					}
					
					
					
				}
				,
				error:function(){
					bsMsgServ.errorSlide(codeDef.getConfig({
						type:'operate',
						code:'error'
					}))
					$(e.currentTarget).removeClass('disabled');
				}
			})
		}
		
		,
		/**
		 * 导出word
		 * 
		 */
		editExportWord:function(e){			
					
			var _id = $('#'+rootid + ' .main-form #id').val();
			
			if(_id){
				var _url = '/'+init.getSite() +'/'+ current_bizcode +'/query/exportWord.action';
				var form = $("<form>");
				form.attr('style','display:none');
				form.attr('target','');
				form.attr('method','post');
				form.attr('accept-charset','utf-8');
				form.attr('action',_url);
				var input1 = $("<input>");
				input1.attr('type','hidden')
				input1.attr('name','id')
				input1.attr('value',_id);
				$('body').append(form);
				$(form).append(input1);
				
				form.submit();
				form.remove();
			}
			$(e.currentTarget).removeClass('disabled');
		}
		
		
    		
	}
	
	var public_method = {
		initList:function(){
			rootid = 'issue-main';
			list_c = ' .list-detail';
			isView = false;
			isScriptOpen = false;
			
			scriptParam = null;
			private_method.initNav();
		}
		,
		
		/**
		 * 外部容器加载流程业务列表数据
		 * @param _options.rootid		dom根节点对象
		 * @param _options.bizcode		流程业务代码
		 * @param _options.queryParam	初始查询条件
		 * @param _options.btnContainer	按钮容器class
		 * @param _options.listContainer	列表对象容器
		 */
		initOutList:function(_options){
			if(_options && _options.rootid && _options.bizcode){
				rootid = _options.rootid;
				
				current_bizcode = _options.bizcode;
				
				private_method.initOutList(_options);
				
				//缓存out list 参数				
				scriptParam = _options;
				
				//设置脚本标志
				isScriptOpen = true;
			}
		}
		,
		/**
		 * 初始化编辑页面
		 */
		initEdit:function(_options){
			
			if(window.console){
				console.log('init edit page ...')
			}
			private_method.initEdit(_options)
		}
		,
		
		/**
		 * @Param _options.rootid dom跟节点id		 
		 * @Param _options.bizcode	当前业务代码
		 * @Param _options.data		当前表单数据
		 * @Param _options.saveCallback	保存回调函数
		 */
		openAddPage:function(_options){
			
			if(window.console){
				console.log('open add page ...')
			}
			if(_options.rootid){
				rootid = _options.rootid;
			}			
			
			isView = false;
			
			if(_options.bizcode){
				current_bizcode = _options.bizcode;
			}
			
			if(_options.data){
				curent_selectRow = $.extend({}, _options.data,{plan_id:_options.data.id});
				delete curent_selectRow['id'];
				delete curent_selectRow['bizcode'];
				delete curent_selectRow['bizno'];
			}
			
			isScriptOpen = true;
			
			scriptParam = _options;
		},
		
		/**
		 * @Param _options.rootid dom跟节点id		 
		 * @Param _options.bizcode	当前业务代码
		 * @Param _options.flowInstId	当前表单流转唯一id
		 * @param _options.isView 		查看模式显示
		 * @Param _options.saveCallback	保存回调函数
		 */
		openEditPage:function(_options){
			
			if(window.console){
				console.log('open edit page ...')
			}
			if(_options.rootid){
				rootid = _options.rootid;
			}			
			
			isView = _options.isView;	
			
			
			if(_options.bizcode && _options.flowInstId){
				current_bizcode = _options.bizcode;
				
				bsUtil.getflowData({
					flowInstId	: _options.flowInstId,
					bizcode		: _options.bizcode,
					callback	: function(data){
						curent_selectRow = data;					
					}
				})
				
			}
			
			isScriptOpen = true;
			
			scriptParam = _options;
		},
		destory:function(){
			//取消签名绑定事件
			var _el = $('#'+rootid + ' .main-form .sign-user')
			_el.unbind('click');
		}
		
	}
	
	return public_method;
		
    
})
